package com.example.openademo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.AlibabaSDK;
import com.alibaba.sdk.android.openaccount.callback.LoginCallback;
import com.alibaba.sdk.android.openaccount.model.OpenAccountSession;
import com.alibaba.sdk.android.openaccount.ui.OpenAccountUIService;
import com.aliyun.alink.business.alink.ALinkResponse;
import com.aliyun.alink.business.login.IOpenLoginAdaptor;
import com.aliyun.alink.business.login.OpenLoginBusiness;
import com.example.activity.Login_DemoActivity;
import com.example.activity.WeexActivity;

import java.util.Map;

/**
 * Created by Administrator on 2016-7-23.
 */
public class OpenAActivity extends Activity {



    public Button button1;
    public Button button2;
    private  CustomLogin customLogin = new CustomLogin();
    public TextView loginText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.clickButton);
        button1.setOnClickListener(new ClickBtnListener());
        button2 = (Button) findViewById(R.id.clickLoadUI);
        button2.setOnClickListener(new btnListener());
        loginText = (TextView) findViewById(R.id.loginText);
        loginText.requestFocus();
        OpenLoginBusiness.init(this,customLogin);
    }

    /**
     * Demo Click Function
     */
    class ClickBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(OpenAActivity.this, WeexActivity.class);
            startActivity(intent);
        }
    }


    class btnListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            OpenAccountUIService openAccountUIService = AlibabaSDK.getService(OpenAccountUIService.class);
            openAccountUIService.showLogin(OpenAActivity.this, new LoginCallback() {
                @Override
                public void onSuccess(OpenAccountSession openAccountSession) {
                    customLogin.authCode = openAccountSession.getAuthorizationCode();
                    //Toast.makeText(OpenAActivity.this, "登录成功 UserID" + openAccountSession.getUserId(), Toast.LENGTH_SHORT).show();
                    loginText.setText("登录成功");
                    OpenLoginBusiness.loginAlink();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(OpenAActivity.this, "登录失败 = " + s, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }

    private class CustomLogin implements IOpenLoginAdaptor{

        public  String authCode = null;

        @Override
        public String getAuthCode() {
            return this.authCode;
        }

        @Override
        public String getAppDeviceID() {
            return null;
        }

        @Override
        public void loginAlinkSucc() {
            Toast.makeText(OpenAActivity.this, "Login Success", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void loginAlinkFail(ALinkResponse aLinkResponse) {
            Toast.makeText(OpenAActivity.this, "Login Fail" + ALinkResponse.getJSONString(aLinkResponse), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void needReLogin() {
            Toast.makeText(OpenAActivity.this, "未登录或自动登录失效，需要重新登录！！", Toast.LENGTH_SHORT).show();
        }
    }
}
